package com.sykj.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sykj.api.JavaSmsApi;
import com.sykj.exception.BizException;
import com.sykj.util.redis.RedisUtil;
import com.sykj.util.reponse.CommonEnum;
import com.sykj.util.reponse.ResultBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

/**
 * @program: BeautyClub
 * @description:
 * @author: qiaoli
 * @create: 2020-07-21 10:22
 */
@Service
public class SmsApiServiceImpl {


    /**
     * 功能描述: 生成验证码发送至手机号
     * @Param: [phone]
     * @Return: java.lang.String
     * @Author: Feiwo Sicun
     * @Date: 2020/7/21 10:24
     */
    public Integer  sendCode(String phone)  {

        int a=(int)((Math.random()*9+1)*10000);
        String s = JavaSmsApi.sendSms(phone, a + "");
        ObjectMapper mapper = new ObjectMapper();
        try {
            Map<String,Object> m = mapper.readValue(s, Map.class);
            String code =  m.get("code").toString();
            System.err.println(m.toString());
            if(code.equals("0")){
                System.err.println("true");
              boolean set = RedisUtil.set(phone+":yzm"  , a, 60);
              if(set){
                  return a;
              }else {
                  return null;
              }


            }else {
                return  null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }



return  null;

    }
    /**
     * 功能描述: 测试环境 ---验证码
     * @Param: [phone]
     * @Return: boolean
     * @Author: Feiwo Sicun
     * @Date: 2020/7/21 10:35
     */
    public Integer  TestsendCode(String phone)  {

        int a=(int)((Math.random()*9+1)*10000);
                boolean set = RedisUtil.set(phone+":yzm"  , a, 30*60);
                if(set){
                    System.err.println(a+"--------------");
                    return a;
                }else {
                    return null;
                }
    }
    
    /**
     * 功能描述: 
     * @Param:  判断账号登录  错误次数
     * @Return: 
     * @Author: Feiwo Sicun
     * @Date: 2020/7/23 14:25
     */
    public  CommonEnum    Check(String phone,String code){
        Boolean exit=RedisUtil.hasKey(phone+":lock");
        //判断是否有此账号
        if(exit){
            String  lock=RedisUtil.get(phone + ":lock")+"";
            if("true".equals(lock)){
                return  CommonEnum.REDIS_LOCK_ERROR;
            }else if("5".equals(lock)){
                RedisUtil.set(phone+":lock","true");
                RedisUtil.expire(phone+":lock",60);
                return  CommonEnum.REDIS_LOCK_ERROR;
            }else{
                return  Detemine(phone,code);
            }

        }else{
            return  Detemine(phone,code);
    }





    }
// 判断验证码是否正确
    public   CommonEnum  Detemine(String phone,String code){
        String o =  RedisUtil.get(phone + ":yzm")+"";
        if(code.equals(o)){
                if(RedisUtil.hasKey(phone+":lock")){
                    RedisUtil.del(phone+":lock");
                }
                RedisUtil.del(phone+":yzm");

                return CommonEnum.SUCCESS;
            }else{
                if(RedisUtil.hasKey(phone+":lock")){
                    RedisUtil.incr(phone+":lock",1);
                    return  CommonEnum.SYS_USER_LONGE_NO;
                }else{

                    RedisUtil.setex(phone+":lock");
                    return CommonEnum.SYS_USER_LONGE_NO;
                }
            }

    }
}
